package br.com.weysolucoes.managedBean;

import java.sql.SQLException;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import br.com.weysolucoes.conexao.ConexaoBD;
import br.com.weysolucoes.dao.ResolucaoNcDAO;
import br.com.weysolucoes.dto.ResolucaoNcDTO;

@ManagedBean(name = "ResolucaoMB")
public class ResolucaoNcManagedBean {

	private ResolucaoNcDTO resolucaoNcDTO = new ResolucaoNcDTO();
	private ResolucaoNcDAO dao = new ResolucaoNcDAO();
	private DataModel<ResolucaoNcDTO> listarResolucoes;

	public ResolucaoNcDTO getResolucaoNc() {
		return resolucaoNcDTO;
	}

	public void setResolucaoNc(ResolucaoNcDTO resolucaoNcDTO) {
		this.resolucaoNcDTO = resolucaoNcDTO;
	}

	// Seleciona Linha da listagem e armazena no objeto para ser recarregado na
	// edi��o dos dados.

	public void sel() {
		resolucaoNcDTO = listarResolucoes.getRowData();
	}

	public String cadastrarResolucaoNc() throws SQLException {

		ConexaoBD conexaoBD = new ConexaoBD();
		ResolucaoNcDAO con = new ResolucaoNcDAO();

		if (con.inserirResolucaoNc(resolucaoNcDTO)) {
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_INFO, "Sucesso!",
							"Resolucao cadastrado com sucesso!"));
		} else {
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erro!",
							"Erro no cadastro de Resolucao!"));
		}
		conexaoBD.closeConnection();
		return "listar_ResolucaoNc.jsf";
	}

	public String deletarResolucaoNc() throws SQLException {

		ResolucaoNcDAO dao = new ResolucaoNcDAO();
		ConexaoBD conexaoBD = new ConexaoBD();

		if (dao.deletarResolucaoNc(resolucaoNcDTO)) {
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_INFO, "Sucesso!",
							"Resolucao Deletada com sucesso!"));
		} else {
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erro!",
							"Erro na exclus�o da Resolucao!"));
		}
		conexaoBD.closeConnection();
		return "listar_ResolucaoNc.jsf";
	}

	public String editarResolucaoNc() throws SQLException {

		ConexaoBD conexaoBD = new ConexaoBD();
		ResolucaoNcDAO dao = new ResolucaoNcDAO();

		if (dao.editarResolucaoNc(resolucaoNcDTO)) {
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_INFO, "Sucesso!",
							"Resolucao Editada com sucesso!"));
		} else {
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erro!",
							"Erro na edi��o da Resolucao!"));
		}
		conexaoBD.closeConnection();
		return "listar_ResolucaoNc.jsf";
	}

	public DataModel<ResolucaoNcDTO> getListarResolucoes() throws SQLException {
		dao = new ResolucaoNcDAO();
		List<ResolucaoNcDTO> lista = dao.listResolucaoNcDTO();
		listarResolucoes = new ListDataModel<ResolucaoNcDTO>(lista);
		return listarResolucoes;
	}

}


